package de.tum.beilmeiner.im.server.dao.impl;

import java.util.LinkedHashMap;
import java.util.List;

import de.tum.beilmeiner.im.server.dao.UserDao;
import de.tum.beilmeiner.im.shared.model.User;

public class UserDaoImpl implements UserDao {

	public static UserDaoImpl INSTANCE = new UserDaoImpl();
	private LinkedHashMap<String, User> allUsers = null;
	private LinkedHashMap<String, User> loggedInUsers = null;
	
	private UserDaoImpl()
	{
		allUsers = new LinkedHashMap<String,User>();
		loggedInUsers = new LinkedHashMap<String,User>();
		insertDummyData();
	}
	
	private void insertDummyData() {
		User franz = new User("franz", "password", "franz", "beilmeier");
		User hubert = new User("hubert", "password", "hubert", "mueller");
		User heino = new User("heino", "password", "heino", "hanswurst");
		User helmut = new User("helmut", "password", "helmut", "kohl");
		allUsers.put(franz.getUserId(), franz);
		allUsers.put(hubert.getUserId(), hubert);
		allUsers.put(heino.getUserId(), heino);
		allUsers.put(helmut.getUserId(), helmut);
		
		
		
	}

	public static UserDaoImpl getInstance()
	{
	return INSTANCE;	
	}
	
	@Override
	public User getUserbyIdAndPassword(String userid, String password) {
		User loginuser = allUsers.get(userid);
		if (loginuser != null)
		{
			if (loginuser.getPassword().equals(password))
			{
			loggedInUsers.put(loginuser.getUserId(), loginuser);	
			return loginuser;	
			}
		}
		return null;
	}

	@Override
	public LinkedHashMap<String, User> getAllUser() {
		return allUsers;
	}

	@Override
	public LinkedHashMap<String, User> getLoggedInUser() {
		return loggedInUsers;
	}

	@Override
	public void addUser(User user) {
		allUsers.put(user.getUserId(), user);

	}

	@Override
	public void removeUser(User user) {
	allUsers.remove(user.getUserId());
}

	public void logoutUser(User logoutUser) {
		System.out.println("userDao logout user "+logoutUser.getUserId());
		loggedInUsers.remove(logoutUser.getUserId());
	}

}
